<?php

/**
 * @param $name
 * @param string $value
 * @param string $className
 * @param string $placeholder
 * @return string
 */
function form_input_text($name, $value="", $className='form-control', $placeholder=''){
    return sprintf('<input type="text" name="%s" value="%s" class="%s" placeholder="%s"/>',
        $name,
        $value,
        $className,
        $placeholder
    );
}

/**
 * @param $name
 * @param string $value
 * @param string $className
 * @param string $placeholder
 * @return string
 */
function form_input_hidden($name, $value="", $className='form-control', $placeholder=''){
    return sprintf('<input type="hidden" name="%s" value="%s" class="%s" placeholder="%s"/>',
        $name,
        $value,
        $className,
        $placeholder
    );
}

/**
 * @param $name
 * @param string $value
 * @param string $className
 * @param string $placeholder
 * @param int $rows
 * @return string
 */
function form_textarea($name, $value="", $className='form-control', $placeholder='', $rows = 3){
    return '<textarea name="'.$name.'" class="'.$className.'" placeholder="'.$placeholder.'" rows="'. $rows.'"/>'.$value.'</textarea>';
}

/**
 * @param $name
 * @param string $currentValue
 * @param string $className
 * @param string $options
 * @return string
 */
function form_select($name, $currentValue="", $className='form-control', $options=''){
    // print_r($options);exit;
    $html = '<select name="'.$name.'" class="'.$className.'">';
    $html .= '<option value="0">-- 默认 --</option>';
    foreach ($options as $key => $val) {
        $isSelected = ($currentValue == $key) ? 'selected="selected"' : '';
        $html .= '<option value="'.$key.'" '.$isSelected.'>'.$val .' ('. $key .') </option>';
    }
    $html .= '</select>';
    return $html;
}

/**
 * @param string $btnName
 * @param string $btnType
 * @param array $options
 * @return string
 */
function form_submit_buttion($btnName='保存修改', $btnType='button', $options = []) {
    return sprintf('<button id="formSubmitBtn" type="%s" name="commit" class="btn btn-success">%s</button>',
        $btnType,
        $btnName
    );
}

/**
 *
 * @param array $formFields
 * @param array $formOptions
 * @return string
 */
function form_create($formFields = [], $formOptionList = [], $formValueList = []) {
    // print_r($formOptions);
    $editFields  = $formFields['edit_fields'] ?? [];
    $editButtons = $formFields['edit_buttons'] ?? [];

    $html = '<form id="myForm" accept-charset="UTF-8" class="simple_form form-horizontal" method="post" novalidate="novalidate">';
    $html .= '<input type="hidden" name="_submit" value="1" />';
    $html .= csrf_field();

//     print_r($formValueList);exit;
    foreach ($editFields as $fieldName => $rows) {
 
//        $currentValue = isset($options[$fieldName]) ? $options[$fieldName] : '';
        if ($rows['element'] == 'input-text'){
            $currentValue = isset($formValueList[$fieldName]) ? $formValueList[$fieldName] : '';

            $data = form_input_text($rows['name'], $currentValue, $rows['className'], $rows['placeholder']);
        } elseif ($rows['element'] == 'select'){
            $currentValue = isset($formValueList[$fieldName]) ? $formValueList[$fieldName] : '';
            $options = isset($formOptionList[$fieldName]) ? $formOptionList[$fieldName] : [];
 
            $data = form_select($rows['name'], $currentValue, $rows['className'], $options);
        } elseif ($rows['element'] == 'textarea'){
            // $options = isset($options[$fieldName]) ? $options[$fieldName] : [];
            $currentValue = isset($formValueList[$fieldName]) ? $formValueList[$fieldName] : '';
//print_r($currentValue);exit;
            $data = form_textarea($rows['name'], $currentValue, $rows['className'], $rows['placeholder']);
        } 
        // function form_input_text($name, $value="", $className='form-control', $placeholder=''){

        $html .= '<div class="form-group">
                <label class="col-md-3 control-label" for="source_link">'. $rows['title'] .'</label>
                <div class="col-md-8">
                '. $data .'</div></div>';
    }

    $btnName = ! empty($editButtons['add_button']) ? $editButtons['add_button'] : '提交保存';
    $html .= sprintf('<div class="form-group"><div class="col-md-offset-3 col-md-9">%s</div></div>',
        form_submit_buttion($btnName)
    );
    $html .= '</form>';
    return $html;
}
